package com.example.demo.controller;


import com.example.demo.config.ErrorResolve.FrontResult;
import com.example.demo.config.ErrorResolve.ResultCodeEnum;
import com.example.demo.config.ErrorResolve.ResutlMsgEnum;
import com.example.demo.entity.Huiyuan;
import com.example.demo.mapper.HuiyuanMapper;
import com.example.demo.service.HuiyuanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.mail.imap.protocol.ID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/huiyuan")
@Api(value = "/file", tags = "会员管理")
public class HuiyuanController {
    @Autowired
    HuiyuanService huiyuanService;

    @Autowired
    HuiyuanMapper huiyuanMapper;

    @ApiOperation(value = "获取会员", httpMethod = "GET", produces = "application/json")
    @GetMapping("/getallgrade")
    public FrontResult GetId() {
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanService.getallgrade());
    }

    @ApiOperation(value = "删除会员", httpMethod = "GET", produces = "application/json")
    @GetMapping("/deletegrade")
    public FrontResult GetId(@RequestParam int id) {
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanService.deletegrade(id));
    }

    @ApiOperation(value = "新增会员", httpMethod = "POST", produces = "application/json")
    @PostMapping("/insertgrade")
    public FrontResult InsertDetailImg(@RequestBody Huiyuan huiyuan) {

        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanService.insertgrade(huiyuan));
    }

    @ApiOperation(value = "更新会员", httpMethod = "POST", produces = "application/json")
    @PostMapping("/upgrade")
    public FrontResult Upgrade(@RequestBody Huiyuan huiyuan) {

        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanService.upgrade(huiyuan));
    }

    @ApiOperation(value = "根据name查询会员", httpMethod = "GET", produces = "application/json")
    @GetMapping("/getgradeusername")
    public FrontResult getgradeusername(@RequestParam String name) {

        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanMapper.getgradeusername(name));
    }

    @ApiOperation(value = "获取全部会员分页", httpMethod = "GET", produces = "application/json")
    @GetMapping("/getqbfenye")
    public FrontResult getqbfenye(@RequestParam int i, @RequestParam int pagesize) {
        long nums = huiyuanMapper.getpicfenyes().size();
        PageInfo<Huiyuan> huiyuanPageInfo = new PageInfo<>(huiyuanService.getqbfenye(i, pagesize));
        huiyuanPageInfo.setTotal(nums);
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanPageInfo);
    }

    @ApiOperation(value = "模糊查询名字", httpMethod = "GET", produces = "application/json")
    @GetMapping("/mohu")
    public FrontResult mohu(@RequestParam String name) {
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanMapper.mohu(name));
    }

    @ApiOperation(value = "模糊查询q", httpMethod = "GET", produces = "application/json")
    @GetMapping("/mohu1")
    public FrontResult mohu1(@RequestParam String name) {
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanMapper.mohu1(name));
    }

    @ApiOperation(value = "模糊查询q1", httpMethod = "GET", produces = "application/json")
    @GetMapping("/mohu2")
    public FrontResult mohu2(@RequestParam String name) {
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanMapper.mohu2(name));
    }

    @ApiOperation(value = "模糊查询各种", httpMethod = "GET", produces = "application/json")
    @GetMapping("/mohu3")
    public FrontResult mohu3(@RequestParam String name, @RequestParam String sex, @RequestParam String q, @RequestParam String address, @RequestParam String phone, @RequestParam String memo, @RequestParam Integer i, @RequestParam Integer pagesize) {
        String ab = q;
        long nums;
        if (memo.isEmpty() || memo == null) {

            nums = huiyuanMapper.mohu3B(name, sex, ab, address, phone, memo, i, pagesize).size();

        } else {
            nums = huiyuanMapper.mohu3pss(name, sex, ab, address, phone, memo, i, pagesize).size();

        }

        PageInfo<Huiyuan> huiyuanPageInfo = new PageInfo<>(huiyuanService.mohu3(name, sex, ab, address, phone, memo, i, pagesize));
        huiyuanPageInfo.setTotal(nums);
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanPageInfo);

    }

    @ApiOperation(value = "模糊查询各种1", httpMethod = "GET", produces = "application/json")
    @GetMapping("/mohu4")
    public FrontResult mohu4(@RequestParam String name, @RequestParam String sex, @RequestParam String address, @RequestParam String phone, @RequestParam String memo, @RequestParam Integer i, @RequestParam Integer pagesize) {
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanMapper.mohu4(name, sex, address, phone, memo, i, pagesize));
    }

    @ApiOperation(value = "模糊查询各种1", httpMethod = "GET", produces = "application/json")
    @GetMapping("/mohuid")
    public FrontResult mohuid(@RequestParam Integer id, @RequestParam Integer i, @RequestParam Integer pagesize) {
        long nums = huiyuanMapper.mohuids(id, i, pagesize).size();

        PageInfo<Huiyuan> huiyuanPageInfo = new PageInfo<>(huiyuanService.mohuid(id, i, pagesize));
        huiyuanPageInfo.setTotal(nums);
        return FrontResult.build(ResultCodeEnum.SUCCESS.getCode(),
                ResutlMsgEnum.EXECUTE_SUCCESS.getMsg(), huiyuanPageInfo);
    }

}
